package com.example.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

/**
 * create by Test on 2024/4/22 Time：15:40
 * describe:
 */
public class Cun {


    private void registerBluetoothReceiver() {
        //filter注册广播接收器
        IntentFilter filter = new IntentFilter();
        //蓝牙当前状态
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        //开始扫描蓝牙设备广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        //找到蓝牙设备广播
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        //扫描蓝牙设备结束广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        //蓝牙设备配对状态改变广播
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        //设备扫描模式改变广播
        filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        //  registerReceiver(receiver, filter);

    }


    //处理找到蓝牙设备和搜索完成的广播消息
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //开始查找设备
            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED == action) {
                Log.d("mjt", "receiver 开始查找");
            } else if (BluetoothDevice.ACTION_FOUND == action) {
                //搜到蓝牙设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d("mjt", "receiver 设备名：" + device.getName() + "地址：" + device.getAddress());

            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) {
                //搜索完毕
                //     Toast.makeText(MainActivity.this, "选择要配对的蓝牙设备", Toast.LENGTH_SHORT).show();
                Log.d("mjt", "查找结束");
            }
        }
    };
}

